<?php
/*
if ($_SERVER['REMOTE_ADDR'] != "80.98.80.146") {
    header('HTTP/1.1 301 Moved Permanently');
    header('Location: http://www.hungarianexporters.hu/');
    exit();
}
*/
session_start();

ini_set('session.gc_maxlifetime', 14400);
ini_set('session.cookie_lifetime', 14400);
ini_set('session.gc_divisor', 1000);
ini_set('session.use_cookies', 0);

$_CONFIG = array();
$_ADMIN = array();

$stat = array();
$dataServices = array();
$loadedPlugins = array();
$htmlServices = array();
$popupResult = array();
$popupSubmitScripts = array();
$exposedObjects = array();
$pagejs = array();


if (!isset($_SESSION['sid'])) {
    $_SESSION['sid'] = rand() % 10000;
}

require_once("smarty/smarty.class.php");
require_once("_framework/load.framework.php");

require_once("phpmailer/language/phpmailer.lang-en.php");
require_once("phpmailer/class.phpmailer.php");
require_once("config/loadconfig.php");

$smarty = new Smarty();
$smarty->setTemplateDir("./site");
$smarty->setPluginsDir("./plugins");

$smarty->left_delimiter = "<<";
$smarty->right_delimiter = ">>";

$page = getPageNameFromParameters();

/********************************************************/

Log::info("Request processing started");

$mode = getRequestParameter("mode", "normal");

$isLoggedIn = isset($_SESSION['userdata']);

$smarty->assign("_config", $_CONFIG);
$smarty->assign("loggedin", $isLoggedIn);
$smarty->assign("user", Auth::getDumpUserData());
$smarty->assign("admin", Auth::isAdmin());
$smarty->assign("stat", $stat);
$smarty->assign("hasCompanyProfile", Auth::userHasProfile());
$smarty->assign("registertype", Auth::getUserProperty("registertype"));
$smarty->assign("userdata", $_SESSION['userdata']);
$smarty->assign("links", array(
    companyprofile => "index.php?choice=profile",
));

/**************************************************/
if ($mode == "service") {
/**************************************************/
    $page = getRequestParameter("page");
    $serviceName = getRequestParameter("service");
    require_once("site/pages/$page/$page.php");
    $params = $_POST;
    if (isset($_GET['params'])) {
        $params = array_merge($_POST, unserialize(stripslashes($_GET['params'])));
    }
    foreach ($_GET as $key=>$value) {
        if ($key != "params" && strpos($key, "_") === 0) {
            $params[ltrim($key, "_")] = $value;
        }
    }
    if (in_array($serviceName, $dataServices)) {
        initDatabase();
        $params = sophisticateData($params);
    }
    if (in_array($serviceName, $htmlServices)) {
        echo executeService($serviceName, $params, $smarty);
    } else {
        echo json_encode(executeService($serviceName, $params, $smarty));
    }
/**************************************************/
} else if ($mode == "adminservice") {
/**************************************************/
    $page = getRequestParameter("page");
    $serviceName = getRequestParameter("service");
    require_once("site/administrator/pages/$page/$page.php");
    $params = $_POST;
    if (isset($_GET['params'])) {
        $params = array_merge($_POST, unserialize(stripslashes($_GET['params'])));
    }
    if (in_array($serviceName, $dataServices)) {
        initDatabase();
//        $params = sophisticateData($params);
    }
    if (in_array($serviceName, $htmlServices)) {
        echo executeService($serviceName, $params, $smarty);
    } else {
        echo json_encode(executeService($serviceName, $params, $smarty));
    }
/**************************************************/
} else if ($mode == "admin" && Auth::isAdmin()) {
/**************************************************/
    $params = $_POST;
    if (isset($_GET['params'])) {
        $params = array_merge($_POST, unserialize(stripslashes($_GET['params'])));
    }
    if ($page == "") {
        $dir = "site/administrator/pages";
        $dirh = opendir($dir);
        $pagelist = array();
        while (($file = readdir($dirh)) !== false) {
            if ($file == "." || $file == "..") { continue; }
            if (file_exists($dir . "/" . $file . "/$file.config.php")) {
                $pagefile = $dir . "/" . $file . "/$file.config.php";
                include_once($pagefile);
                $pagelist[$file] = $_ADMIN['pagetitle'];
            }
        }
        closedir($dirh);
        $smarty->assign("pagetitlelist", array_values($pagelist));
        $smarty->assign("pagelist", array_keys($pagelist));
        $smarty->display('administrator/layout/index.tpl');
    } else {
        if (is_dir("site/administrator/pages/$page/resources/js")) {
            $dirh = opendir("site/administrator/pages/$page/resources/js");
            while (($file = readdir($dirh)) !== false) {
                if ($file == "." || $file == "..") { continue; }
                $pagejs[] = "site/administrator/pages/$page/resources/js/$file";
            }
            closedir($dirh);
        }
        require_once("site/administrator/pages/$page/$page.php");
        $smarty->assign("loadedPlugins", $loadedPlugins);
        $smarty->assign("pageJavascripts", $pagejs);
        $smarty->assign("header", getPageHeader());
        $smarty->assign("footer", getPageFooter());
        buildLayout($smarty);

        $layoutName = TemplateHelper::getLayout();
        $smarty->display("administrator/layout/$layoutName/layout.$layoutName.tpl");
    }
/**************************************************/
} else if ($mode == "normal") {
/**************************************************/

    if ($page == "") {
        $smarty->display('layout/index.tpl');
    } else {
        $pagejs = array();
        if (is_dir("site/pages/$page/resources/js")) {
            $dirh = opendir("site/pages/$page/resources/js");
            while (($file = readdir($dirh)) !== false) {
                if ($file == "." || $file == "..") { continue; }
                $pagejs[] = "site/pages/$page/resources/js/$file";
            }
        }
        foreach ($_GET as $key=>$value) {
            if (strpos($key, "_") === 0) {
                $params[ltrim($key, "_")] = $value;
            }
        }
        require_once("site/pages/$page/$page.php");
        $smarty->assign("loadedPlugins", $loadedPlugins);
        $smarty->assign("pageJavascripts", $pagejs);
        $smarty->assign("header", getPageHeader());
        $smarty->assign("footer", getPageFooter());
        buildLayout($smarty);

        $layoutName = TemplateHelper::getLayout();
        $smarty->display("layout/$layoutName/layout.$layoutName.tpl");
    }
/**************************************************/
} else if ($mode == "companyview") {
/**************************************************/
    initDatabase();
    $page = "companyprofile";
    $service = "viewcompany";
    $name = getrequestParameter("name");
    $pagejs = array();
    if (is_dir("site/pages/$page/resources/js")) {
        $dirh = opendir("site/pages/$page/resources/js");
        while (($file = readdir($dirh)) !== false) {
            if ($file == "." || $file == "..") { continue; }
            $pagejs[] = "site/pages/$page/resources/js/$file";
        }
    }
    $params['name'] = $name;
    require_once("site/pages/$page/$page.php");
    $smarty->assign("prefix", $_CONFIG['directories']['baseURL'] . "/");
    $smarty->assign("loadedPlugins", $loadedPlugins);
    $smarty->assign("pageJavascripts", $pagejs);
    $smarty->assign("header", getPageHeader());
    $smarty->assign("footer", getPageFooter());
    buildLayout($smarty);

    $layoutName = TemplateHelper::getLayout();
    $smarty->display("layout/$layoutName/layout.$layoutName.tpl");
}

Log::info("Request processing ended");

Log::closeLog();

/**********************************************************/

?>
